﻿using System;
namespace ORS.OrderRobot
{
    /// <summary>
    /// 自我执行策略
    /// </summary>
	public static class RepeatStrategies
    {
        /// <summary>
        /// 线性增长延迟策略：每次延迟逐步增加。
        /// 示例：base = 5000, step = 2000 -> 5s, 7s, 9s, 11s...
        /// </summary>
        /// <param name="baseMilliseconds">初始延迟（毫秒）</param>
        /// <param name="stepMilliseconds">每次增加的毫秒数</param>
        public static Func<int, TimeSpan> Linear(int baseMilliseconds, int stepMilliseconds) =>
            attempt => TimeSpan.FromMilliseconds(baseMilliseconds + stepMilliseconds * (attempt - 1));

        /// <summary>
        /// 指数增长延迟策略：每次延迟指数级增长。
        /// 示例：base = 2000 -> 2s, 4s, 8s, 16s...
        /// 常用于退避/冷却策略。
        /// </summary>
        /// <param name="baseMilliseconds">基础延迟（毫秒）</param>
        /// <param name="maxMilliseconds">最大允许延迟（毫秒），防止过大</param>
        public static Func<int, TimeSpan> Exponential(int baseMilliseconds, int maxMilliseconds = 60000) =>
            attempt => TimeSpan.FromMilliseconds(
                Math.Min(maxMilliseconds, baseMilliseconds * Math.Pow(2, attempt - 1))
            );

        /// <summary>
        /// 峰值再递减策略（山峰型）：先逐渐增长延迟，然后回落。
        /// 示例：[5,10,15,10,5] 会循环为 5→10→15→10→5→5→10→...。
        /// 适合节奏控制型任务。
        /// </summary>
        /// <param name="patternMilliseconds">自定义延迟毫秒数序列</param>
        public static Func<int, TimeSpan> PeakThenDrop(int[] patternMilliseconds)
        {
            return attempt => TimeSpan.FromMilliseconds(
                patternMilliseconds[(attempt - 1) % patternMilliseconds.Length]
            );
        }
    }
}

